package com.luoshenshen.server;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * @author ShenshenLuo
 * @date 2025/3/22 20:57
 **/
public class NettyClient {

    public static void main(String[] args) {

        EventLoopGroup group = null;
        try {
            group = new NioEventLoopGroup();
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ch.pipeline().addLast(new NettyClientHandler());
                        }
                    });

            System.out.println("client starting...");
            ChannelFuture sync = bootstrap.connect(new InetSocketAddress(8888)).sync();
            System.out.println("client connected...");
            sync.channel().closeFuture().sync();
            System.out.println("client end...");
        }catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            if (group != null) {
                group.shutdownGracefully();
            }
        }

    }
}
